package bylw.controller;

import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import bylw.entity.Drug;
import bylw.entity.Url;
import bylw.service.Service;

/*
 * 应该把GET,POST,PUT,DELETE四个方法都实现，尽量把项目做的精小。-->麻雀虽小，五脏俱全！
 */
@Controller
public class GlobalController {
	private Service service;

	@Autowired
	public void setService(Service service) {
		this.service = service;
	}

	/*
	 * 根据药名搜索资源
	 */
	@RequestMapping(value = "search/drug/{name}", method = RequestMethod.GET)
	public String getDrugList(@PathVariable("name") String name, Model model) {
		model.addAttribute("drugList", this.service.getSearchResult(name));
		return "manager/url";
	}

	/*
	 * 增加一个Drug
	 */
	@RequestMapping(value = "drug", method = RequestMethod.POST, headers = { "Accept=application/json" })
	public String addDrug(Drug drug, Model model) {
		try {
			this.service.addDrug(drug);
			model.addAttribute("result", "添加成功！");
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("result", "添加失败！");
		}

		return null;
	}

	/*
	 * 增一个Url
	 */
	@RequestMapping(value = "url", method = RequestMethod.POST, headers = { "Accept=application/json" })
	public String addUrl(Url url) {
		return null;
	}

	/*
	 * 更新一个Url
	 */
	@RequestMapping(value = "url", method = RequestMethod.PUT, headers = { "Accept=application/json" })
	public String updateUrl(Url url) {
		return null;
	}

	/*
	 * 删除一个Url
	 */
	@RequestMapping(value = "url", method = RequestMethod.DELETE, headers = { "Accept=application/json" })
	public String updateUrl(int id) {
		return null;
	}

	/*
	 * 取得Drug列表
	 */
	@RequestMapping(value = "drug/{pageNo}/{pageSize}", method = RequestMethod.GET)
	public String getDrugList(@PathVariable("pageNo") int pageNo,
			@PathVariable("pageSize") int pageSize, Model model) {

		model.addAttribute("pageModel", this.service.getDrugList(pageNo,
				pageSize));

		return "manager/drug";
	}

	/*
	 * 取得Url列表
	 */
	@RequestMapping(value = "url/{pageNo}/{pageSize}", method = RequestMethod.GET)
	public String getUrlList(@PathVariable("pageNo") int pageNo,
			@PathVariable("pageSize") int pageSize, Model model) {

		model.addAttribute("pageModel", this.service.getUrlList(pageNo,
				pageSize));

		return "manager/url";
	}

	/*
	 * 模拟外站资源
	 */
	@RequestMapping(value = "external/drug/{name}", method = RequestMethod.GET)
	public @ResponseBody
	Drug getDrugFromExternal(@PathVariable("name") String name,
			HttpServletResponse response) {
		Drug drug = new Drug();
		drug.setName(name);
		drug.setDescription("json encoding?");
		return drug;
	}

}
